package com.example.myprojectoffeixue;

import android.app.Activity;
import android.content.Intent;
import android.net.wifi.ScanResult;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class SetYindaoActivity extends Activity {
    WifiAdmin wifiAdmin;
    private ArrayList<ScanResult> mWifiList;
    String str;
    MyApplication m;
    Spinneradapter sp5;
    ArrayList<String> wifi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_yindao);
        info();
    }

    public void info() {
        m = (MyApplication) getApplication();
        Spinner sp = (Spinner) findViewById(R.id.sp_ato2);
        wifiAdmin = new WifiAdmin(this);
        wifiAdmin.openWifi();
        wifiAdmin.startScan();
        mWifiList = (ArrayList<ScanResult>) wifiAdmin.getWifiList();
       wifi = new ArrayList<String>();

        for (int i = 0; i < mWifiList.size(); i++) {
            if (mWifiList.get(i).SSID.length() < 9) {
                continue;
            } else {
                String m_SSID = mWifiList.get(i).SSID.substring(0, 9);
                if ("FeiXueKJ_".equals(m_SSID)) {
                    wifi.add(mWifiList.get(i).SSID);
                }
            }
        }
        sp5 = new Spinneradapter(wifi, SetYindaoActivity.this);
        sp.setAdapter(sp5);
        sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                str = parent.getItemAtPosition(position).toString();
                m.SSID = str;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {


            }
        });


        TextView refrsh= (TextView) findViewById(R.id.refrsh);
        refrsh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                wifi.clear();
                mWifiList.clear();
                wifiAdmin.startScan();
                mWifiList = (ArrayList<ScanResult>) wifiAdmin.getWifiList();
                for (int i = 0; i < mWifiList.size(); i++) {
                    if (mWifiList.get(i).SSID.length() < 9) {
                        continue;
                    } else {
                        String m_SSID = mWifiList.get(i).SSID.substring(0, 9);
                        if ("FeiXueKJ_".equals(m_SSID)) {
                            wifi.add(mWifiList.get(i).SSID);
                        }
                    }
                }
                sp5.notifyDataSetChanged();
                ToastUtils.showToast(getBaseContext(),"更新完成");
            }

        });

    }


    public void youxian(View v) {
        if (str != null) {

            Intent it = new Intent(SetYindaoActivity.this, Yindao2Activity.class);
            startActivity(it);
            finish();
        } else {

            HttpConnection.showToast(SetYindaoActivity.this,"未发现网关");

        }

    }

    public void wifi(View v) {
        if (str != null) {
            Intent it = new Intent(SetYindaoActivity.this, SetWifiActivity.class);
            startActivity(it);
            finish();
        } else {
        HttpConnection.showToast(SetYindaoActivity.this,"未发现网关");
        }
    }

}
